package cn.stylefeng.guns.modular.shi.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.shi.entity.TaoHelp;
import cn.stylefeng.guns.modular.shi.model.params.TaoHelpParam;
import cn.stylefeng.guns.modular.shi.service.TaoHelpService;
import cn.stylefeng.guns.unitl.ResponseMsg;
import cn.stylefeng.guns.unitl.UploadFileUtils;
import cn.stylefeng.guns.unitl.UploadResult;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.util.Date;


/**
 * 帮助中心控制器
 *
 * @author shiboshi
 * @Date 2019-10-22 09:34:03
 */
@Controller
@RequestMapping("/taoHelp")
public class TaoHelpController extends BaseController {

    private static final String SystemConstant = "https://hybn-1259399233.cos.ap-chengdu.myqcloud.com/";

    String images = "";

    private String PREFIX = "/taoHelp";

    @Autowired
    private TaoHelpService taoHelpService;

    /**
     * 跳转到主页面
     *
     * @author shiboshi
     * @Date 2019-10-22
     */
    @RequestMapping("")
    public String index() {
        images = "";
        return PREFIX + "/taoHelp.html";
    }

    /**
     * 新增页面
     *
     * @author shiboshi
     * @Date 2019-10-22
     */
    @RequestMapping("/add")
    public String add() {
        images = "";
        return PREFIX + "/taoHelp_add.html";
    }

    /**
     * 编辑页面
     *
     * @author shiboshi
     * @Date 2019-10-22
     */
    @RequestMapping("/edit")
    public String edit(String id , HttpSession session) {
        session.setAttribute("deleteId",id);
        images = "";
        return PREFIX + "/taoHelp_edit.html";
    }

    /**
     * 新增接口
     *
     * @author shiboshi
     * @Date 2019-10-22
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TaoHelpParam taoHelpParam) {
        String tupian = taoHelpParam.getPics();
        taoHelpParam.setPics(tupian);
        taoHelpParam.setCrateTime(new Date());
        taoHelpService.addTaoHelp(taoHelpParam);
        images = "";
        return ResponseData.success();
    }

    /**
     * 修改数据接口
     *
     * @author shiboshi
     * @Date 2019-10-22
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(TaoHelpParam taoHelpParam) {
        String tupian = taoHelpParam.getPics();
        taoHelpParam.setPics(tupian);
        this.taoHelpService.updateHelp(taoHelpParam);
        images = "";
        return ResponseData.success(200);
    }

    /**
     * 删除接口
     *
     * @author shiboshi
     * @Date 2019-10-22
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(TaoHelpParam taoHelpParam) {
        this.taoHelpService.deleteTaoHelp(taoHelpParam.getId());
        images = "";
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author shiboshi
     * @Date 2019-10-22
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(HttpSession session) {
        TaoHelp detail = this.taoHelpService.QueryHelp(session.getAttribute("deleteId").toString());
        images = "";
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author shiboshi
     * @Date 2019-10-22
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(TaoHelpParam taoHelpParam) {
        images = "";
        return this.taoHelpService.findPageBySpec(taoHelpParam);
    }



    /**
     * 多图片上传接口
     */
    @RequestMapping(method = RequestMethod.POST, path = "/multipleGraph")
    @ResponseBody
    public ResponseData multipleGraph(@RequestPart("file") MultipartFile file, HttpSession session) throws FileNotFoundException {

        if (ToolUtil.isEmpty(file)){
            return  ResponseData.error(201,"上传失败，图片为空");
        }else{
            ResponseMsg msg = UploadFileUtils.uploadFile(file);
            if (msg.getStatus() == 10001){
                UploadResult result = (UploadResult) msg.getResult();
                images+=SystemConstant+result.getPath()+",";
                return  ResponseData.success(200,"上传成功",images);
            }else {
                return  ResponseData.error(202,"上传失败，图片格式错误");
            }
        }
    }




    /**
     * 删除图片接口
     */
    @RequestMapping(method = RequestMethod.POST, path = "/deleteImges")
    @ResponseBody
    public ResponseData updateImges(TaoHelpParam taoHelpParam,HttpSession session, HttpServletRequest request, @RequestParam(value = "img", required = false) String img) {
        taoHelpParam.setPics(img);
        taoHelpParam.setId(Integer.parseInt(session.getAttribute("deleteId").toString()));
        taoHelpService.updateImges(taoHelpParam);
        return ResponseData.success("删除成功");
    }



}


